import Vue from 'vue'
import Vuex from 'vuex'
// import socket from '../config/socket'
import {SOCKET_BADGE_DATA , TRIGGER_SOCKET_WATHCT_BADGE , REMIDN_USER_SOCKET_BADGE , REMOVE_TABAR_BADGE} from  './mutation-types'
const {baseUrl} = require('../config/env')
const io = require('weapp.socket.io')
const socket = io(baseUrl)

// 监听服务器socket返回
socket.on('showBadge', data => {
  console.log('received news: ', data)
  store.commit(SOCKET_BADGE_DATA,data);
  if ((data.comment + data.praise + data.teach) <= 0) {
      wx.removeTabBarBadge({index: 4})
  } else {
      wx.setTabBarBadge({index: 4,text: (data.comment + data.praise+data.teach).toString()})
  };
  
})


Vue.use(Vuex);
const store = new Vuex.Store({
        // 定义状态
    state: {
      globalBadge: {},
      triggerSocketWatchBadge: ''
    },
    mutations: {
      // 数据映射
      SOCKET_BADGE_DATA (state, data) {
        console.log(data,'.........globalBadge')
        state.globalBadge = data;
      },
      // 获取badge
      TRIGGER_SOCKET_WATHCT_BADGE (state, data) {
        let student_id = wx.getStorageSync('student_id');
        if (student_id) {
            socket.emit('showBadge', { student_id: student_id });
        }
        // socket.emit('removeBadge',data)
      },
      // 提醒对方他有新的badge
      REMIDN_USER_SOCKET_BADGE (state,student_id) {
        socket.emit('remind',{student_id})
      },
      // 删除自己的badge
      REMOVE_TABAR_BADGE (state,data) {
        console.log(data,'.............删除自己的badge')
        socket.emit('removeBadge',data)
      }
    }
})

export default store
